<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 用户模型
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2015-11-19
 +------------------------------------------------------------------------------
 */
namespace Home\Model;
use Home\Model\BaseModel;

class CustomerModel extends BaseModel {
    protected $_validate = array(
        array('login_name', 'require', '登录帐号不能为空', self::EXISTS_VALIDATE, 'regex'),
        array('login_name', '', '登录帐号已经存在', self::VALUE_VALIDATE, 'unique'),
    	array('login_name', '1,20', '登录帐号不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
//     	array('login_pwd', 'require', '登录密码不能为空', self::EXISTS_VALIDATE, 'regex'),
    );

    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', '1', self::MODEL_BOTH),
    );

    /**
     * @todo 用户登陆
     * @param string(11) login_name 登陆账号
     * @author PHP@妖孽 <dino_ma@163.com>
     */
    public function autoLogin($login_name){
        $customer_id = D('Admin/Customer','Service')->register($login_name,null,$login_name,'',6);
        if(!$customer_id){
            $this->error = $this->getError();
            return false;
        }
        /* 记录登录SESSION和COOKIES */
        $auth = array(
            'customer_id'             => $customer_id,
            'login_name'        => $login_name,
            'mobile_phone'        => $login_name,
        );
//         session('[destroy]');
        session('customer_auth', $auth);
        session('customer_auth_sign', data_auth_sign($auth));
        return $customer_id;
    }

    /**
     * @todo 获取手机订单
     * @param $page int 分页码
     * @author mahuan <mahuan1768@126.com>
     */
    public function getMobileOrder($order_list){
        if(empty($order_list)){
            return $order_list;
        }
        $order_status_map = array('待支付','支付失败','已付款','已退款','已消费','申请核销','核销完成','已结算','申请退款');
        foreach($order_list['data']['list'] as $key=>$value){
            $order_list['data']['list'][$key]['create_time'] = isset($value['create_time'])?date('Y-m-d H:i:s',$value['create_time']):'未知';
            $order_list['data']['list'][$key]['order_status_txt'] = isset($value['order_status'])?$order_status_map[$value['order_status']]:'未知';
            //如果状态值为0 待支付和1 支付失败则给出链接
            if(isset($value['order_status']) && in_array($value['order_status'], array(0,1))){
                $order_list['data']['list'][$key]['pay_link'] = U('Shop/pay',array('id'=>$value['order_id']));
            }else{
                $order_list['data']['list'][$key]['pay_link'] = '';
            }
        }
        return $order_list;
    }

    /**
     * @todo 用户登出
     * @author PHP@妖孽 <dino_ma@163.com>
     */
    public function autoLoginOut(){
        //预留接口
        return true;
    }

}
